package com.baboba.rummy.web.client.card;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.baboba.rummy.card.CardComparator;
import com.baboba.rummy.card.UserCard;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;

public class CardListView extends Composite {
    private LinkedList<UserCard> cards;
	private HorizontalPanel topPanel;
	
	public CardListView(){
		this(new LinkedList<UserCard>());
	}
    
    public CardListView(LinkedList<UserCard> cards){
        this.cards = cards;
        
        topPanel = new HorizontalPanel();
        redrawCards();

        this.initWidget(topPanel);
    }
    
    public void addNewCards(List<UserCard> cards){
    	this.cards.addAll(cards);
    	redrawCards();
    }
    
    public void addNewCard(UserCard card){
    	this.cards.add(card);
    	redrawCards();
    }

    public LinkedList<UserCard> getCards() {
    	return cards;
    }
    
    protected void redrawCards(){
    	topPanel.clear();
    	Collections.sort(cards, new CardComparator());
    	for (UserCard card:cards)
        	topPanel.add(new CardView(card));
    }
}
